import 'package:flutter/material.dart';
import 'package:flutterdemo/day02/01_statelesswidget.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("商品内容"),
      ),
      body: JKHomeContent("小李子"),
    );
  }
}

class JKHomeContent extends StatefulWidget {
  final String message;
  JKHomeContent(this.message);
  @override
  _JKHomeContentState createState() => _JKHomeContentState();
}
/**
 * 为什么 Flutter 在设计的时候 StatefulWidget 的build方法放在 State中
 * 1.build出来的Widget是需要依赖State中的变量(状态/数据)
 * 2.在Flutter的运行过程中：Widget 是不断的销毁和创建的，当我们自己的状态发生改变时，并不希望重新状态一个新的State
 * */
class _JKHomeContentState extends State<JKHomeContent> {
  int _counter = 0;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: () {
                  setState(() {
                    _counter++;
                  });
                },
                child: Text("+", style: TextStyle(fontSize: 22, color: Colors.white),),
                color: Colors.pink,
              ),
              SizedBox(
                width: 8,
              ),
              RaisedButton(
                  child: Text("-", style: TextStyle(fontSize: 22, color: Colors.white),),
                  color: Colors.pink,
                  onPressed: () {
                    setState(() {
                      _counter--;
                    });
                  }
              )
            ],
          ),
          Text("当前计数：$_counter", style: TextStyle(fontSize: 22, color: Colors.green),),
          Text("传递进来的信息是：${widget.message}", style: TextStyle(fontSize: 25, color: Colors.brown),),
        ],
      ),
    );
  }
}

